package es.upv.tricia;

import java.util.ArrayList;

import android.util.Log;
import es.upv.tricia.rest.AsyncHttpResponseHandler;
import es.upv.tricia.rest.GsonHttpResponseHandler;
import es.upv.tricia.rest.RequestParams;
import es.upv.tricia.rest.RestClient;

public class RestGame implements Game
{
	private final static String TAG = "RestGame";

	private String email;

	private ArrayList<GameListener> listeners = new ArrayList<GameListener>();

	@Override
	public void initialize(String email)
	{
		this.email = email;
	}

	private void onGameFailed(Throwable t)
	{
		for (GameListener listener : listeners) {
			listener.onGameFailed(t);
		}
	}

	@Override
	public void start()
	{
		Log.d(TAG, "Starting REST game");

		RequestParams params = new RequestParams();
		params.put("email", email);

		RestClient.post("questions", params, new AsyncHttpResponseHandler() {
			@Override
			public void onSuccess(String response)
			{
				Log.d(TAG, "Starting REST game: got response");

				int numberOfQuestions = Integer.parseInt(response);

				for (GameListener listener : listeners) {
					listener.onGameStart(numberOfQuestions);
				}
			}

			@Override
			public void onFailure(Throwable t)
			{
				onGameFailed(t);
			}
		});
	}

	@Override
	public void loadQuestion(int questionNo)
	{
		Log.d(TAG, "Loading REST question");

		RequestParams params = new RequestParams();
		params.put("email", email);
		params.put("question", Integer.toString(questionNo));

		RestClient.get("questions", params,
				new GsonHttpResponseHandler<Question>(Question.class) {
					@Override
					public void onSuccess(Question question)
					{
						Log.d(TAG, "Loading REST question: got response");

						for (GameListener listener : listeners) {
							listener.onQuestionLoaded(question);
						}
					}

					@Override
					public void onFailure(Throwable t)
					{
						onGameFailed(t);
					}
				});

	}

	@Override
	public void stop()
	{
		Log.d(TAG, "Stopping REST game");

		RequestParams params = new RequestParams();
		params.put("email", email);

		RestClient.delete("questions", params, new AsyncHttpResponseHandler() {
			@Override
			public void onSuccess(String response)
			{
				Log.d(TAG, "Stopping REST game: got response");

				RestGame.this.email = null;

				for (GameListener listener : listeners) {
					listener.onGameStop();
				}
			}

			@Override
			public void onFailure(Throwable t)
			{
				onGameFailed(t);
			}
		});
	}

	@Override
	public void publishScore(String username, int score)
	{
		Log.d(TAG, "Publishing REST score: " + email + ", " + username + ", "
				+ score);

		RequestParams params = new RequestParams();
		params.put("email", email);
		params.put("username", username);
		params.put("score", Integer.toString(score));

		RestClient.put("highscores", params, new AsyncHttpResponseHandler() {
			@Override
			public void onSuccess(String response)
			{
				Log.d(TAG, "Publishing REST score: got response");

				for (GameListener listener : listeners) {
					listener.onPublishScore();
				}
			}

			@Override
			public void onFailure(Throwable t)
			{
				onGameFailed(t);
			}
		});
	}

	@Override
	public void addGameListener(GameListener listener)
	{
		synchronized (listeners) {
			listeners.add(listener);
		}
	}
}
